<template>
  <UploadExcel :on-success="success" />
</template>

<script>
import * as filter from '@/utils/filters'
import { importEmployee } from '@/api/employees'
export default {
  name: 'ImprotIndex',
  methods: {
    async success({ header, results }) {
      const userRelation = {
        '姓名': 'username', // 姓名
        '手机号': 'mobile', // 手机号
        '工号': 'workNumber', // 工号
        '入职日期': 'timeOfEntry', // 入职时间
        '转正日期': 'correctionTime' // 转正日期
      }
      var newArray = results.map(item => {
        var userInfo = {}
        Object.keys(item).forEach(ckey => {
          if (userRelation[ckey] === 'correctionTime' || userRelation[ckey] === 'timeOfEntry') {
            // 后端接口 限制了 不能是字符串 要去转化时间类型
            userInfo[userRelation[ckey]] = filter.formatDate(new Date(this.formatDate(item[ckey], '/')))
          } else {
            userInfo[userRelation[ckey]] = item[ckey]
          }
        })
        return userInfo
      })
      console.log(newArray)
      await importEmployee(newArray)
      this.$router.back()
      this.$message.success('导入成功')
    },
    formatDate(numb, format) {
      const time = new Date((numb - 1) * 24 * 3600000 + 1)
      time.setYear(time.getFullYear() - 70)
      const year = time.getFullYear() + ''
      const month = time.getMonth() + 1 + ''
      const date = time.getDate() - 1 + ''
      if (format && format.length === 1) {
        return year + format + month + format + date
      }
      return year + (month < 10 ? '0' + month : month) + (date < 10 ? '0' + date : date)
    }
  }

}
</script>

<style>

</style>
